# 这是一个测试modbus的脚本

# 输出帮助到终端
import random


def PrintMsg(msg):
    print(msg)


def helpInfo():

    print(
        """
      Usage: python modbus_test.py [options]

      Options:
      -h, --help            show this help message and exit
      -a ADDRESS, --address=ADDRESS
                              Modbus TCP slave address to connect to
      -p PORT, --port=PORT  Modbus TCP port to connect to
      """
    )


# 定义结构体
class ModuleVar:
    in0 = 0
    in1 = 0
    in2 = 0
    in3 = 0
    in4 = 0
    in5 = 0
    in6 = 0
    in7 = 0
    in8 = 0
    in9 = 0
    in10 = 0
    in11 = 0
    in12 = 0

    out0 = 0
    out1 = 0
    out2 = 0
    out3 = 0
    out4 = 0
    out5 = 0
    out6 = 0
    out7 = 0
    out8 = 0
    out9 = 0
    out10 = 0
    out11 = 0
    out12 = 0


def func1():

    # moduleVar.in1  转换成字符串输出  in1 ~ in12 的数值
    moduleVar = ModuleVar()
    str = (
        f"{moduleVar.in1} {moduleVar.in2} {moduleVar.in3} {moduleVar.in4} {moduleVar.in5} "
        f"{moduleVar.in6} {moduleVar.in7} {moduleVar.in8} {moduleVar.in9} {moduleVar.in10} "
        f"{moduleVar.in11} {moduleVar.in12}"
    )
    print(str)

    moduleVar.out0 = random.randint(1, 65000)
    moduleVar.out1 = random.randint(1, 65000)
    moduleVar.out2 = random.randint(1, 65000)
    moduleVar.out3 = random.randint(1, 65000)
    moduleVar.out4 = random.randint(1, 65000)
    moduleVar.out5 = random.randint(1, 65000)
    moduleVar.out6 = random.randint(1, 65000)
    moduleVar.out7 = random.randint(1, 65000)
    moduleVar.out8 = random.randint(1, 65000)
    moduleVar.out9 = random.randint(1, 65000)
    moduleVar.out10 = random.randint(1, 65000)
    moduleVar.out11 = random.randint(1, 65000)
    moduleVar.out12 = random.randint(1, 65000)

    str = (
        f"{moduleVar.out0} {moduleVar.out1} {moduleVar.out2} {moduleVar.out3} {moduleVar.out4} "
        f"{moduleVar.out5} {moduleVar.out6} {moduleVar.out7} {moduleVar.out8} {moduleVar.out9} "
        f"{moduleVar.out10} {moduleVar.out11} {moduleVar.out12}"
    )

    PrintMsg(str)


def main():
    PrintMsg("User code start")
    try:
        func1()
        pass
        PrintMsg("User code end")
    except BaseException as e:
        import traceback

        err_stack = traceback.format_exc()
        PrintMsg("\n-----------\n" + err_stack)

    return 0


if __name__ == "__main__":
    main()
    exit(0)
